package net.sycu.guomy.ems.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sycu.guomy.ems.business.*;
import net.sycu.guomy.ems.entity.*;
import net.sycu.guomy.ems.servlet.BaseServlet;

public class ManageLoginLogsServlet extends BaseServlet {
	LoginLogService service = new LoginLogService();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String sortField = "LoginLogId";
		if (request.getParameter("hidSortField") != null) {
			sortField = this.getParameter("hidSortField");
		}
		String sortString = "LoginLogId ASC";
		if (request.getParameter("hidSortString") != null) {
			sortString = this.getParameter("hidSortString");
		}
		
		if (this.getParameter("hidOperateType").
				compareToIgnoreCase("sorting") == 0) {
			if (sortString.indexOf(sortField) >= 0) {
				if (sortString.indexOf("ASC") >= 0)
					sortString = sortString.replace("ASC", "DESC");
				else
					sortString = sortString.replace("DESC", "ASC");
			}
			else
				sortString = sortField + " ASC";
		}
		
		// 
		LoginLogSearcher searcher = new LoginLogSearcher();
		searcher.setLoginIp(this.getParameter("txtSearchLoginIp"));
		if (this.getParameter("txtSearchLoginDateTimeBegin").length() > 0)
			searcher.setLoginDateTimeBegin(this.getTimestampParameter("txtSearchLoginDateTimeBegin"));
		if (this.getParameter("txtSearchLoginDateTimeEnd").length() > 0)
			searcher.setLoginDateTimeEnd(this.getTimestampParameter("txtSearchLoginDateTimeEnd"));
		searcher.setLoginSearcher(new LoginSearcher());
		searcher.getLoginSearcher().setLoginName(this.getParameter("txtSearchLoginName"));
		
		ArrayList<LoginLogBean> loginLogs = service.search(searcher, sortString);
		
		int rowCount = loginLogs.size();
		int pageCount = 1;

		int pageSize = 10;
		if (request.getParameter("hidPageSize") != null) {
			pageSize = this.getIntegerParameter("hidPageSize");
		}
		if (pageSize <= 1)
			pageSize = 10;
		
		pageCount = rowCount / pageSize + (rowCount % pageSize == 0 ? 0 : 1);
		if (pageCount <= 0)
			pageCount = 1;

		int currentPageIndex = 1;
		if (request.getParameter("hidCurrentPageIndex") != null) {
			currentPageIndex = this.getIntegerParameter("hidCurrentPageIndex");
		}
		if (currentPageIndex <= 0)
			currentPageIndex = 1;
		if (currentPageIndex > pageCount)
			currentPageIndex = pageCount;

		request.setAttribute("currentPageIndex", currentPageIndex);
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("rowCount", rowCount);
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("sortField", sortField);
		request.setAttribute("sortString", sortString);

		request.setAttribute("searcher", searcher);
		request.setAttribute("loginLogs", loginLogs);
		request.getRequestDispatcher("./manageLoginLogs.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
